import re
# C数组字符串 传入的字符串
c_array_str = """
    {0xFF, {0x20, 0x10, 0x00}, 3},
    {0x35, {0x00}, 1},
    {0x2A, {0x00, 0x00, 0x00, 0xb3}, 4},
    {0x2B, {0x00, 0x00, 0x01, 0xc5}, 4},
    {0xFF, {0x20, 0x10, 0x00}, 3},
"""
# 去除注释
c_array_str = re.sub(r'/\*.*?\*/', '', c_array_str, flags=re.DOTALL)

# 使用正则表达式匹配并提取数据
pattern = r'{0x([0-9a-fA-F]+),\s*{((?:\s*0x[0-9a-fA-F]+,*)*)},\s*(\d+)\s*}'

matches = re.findall(pattern, c_array_str)

lcd_init_cmds = []
for match in matches:
    command = int(match[0].strip(), 16)
    data = [int(x.strip(), 16) for x in match[1].split(',')]
    data_length = len(data)
    lcd_init_cmds.append({'command': command, 'data': data, 'data_length': data_length})

def convert_and_print_commands(commands):
    for i, command in enumerate(commands):
        data_length = command['data_length']

        # 打印修改后的命令 如需修改在print上进行修改
        for j in range(data_length):
            cmd_hex = command['data'][j]
            print(f"data[{j}] = 0x{cmd_hex:02X};")
        print(f"spi_write(0x{command['command']:02X},data,{data_length});")

convert_and_print_commands(lcd_init_cmds)